const RemoteAgent = require('./remoteAgent');
const net = require('net');

async function main() {
    const config = {
        host: 'remote_host',
        port: 22,
        username: 'user',
        password: 'password' // or use privateKey and passphrase
    };

    const agent = new RemoteAgent(null, config);

    try {
        await agent.connect();

        const localHost = '127.0.0.1';
        const localPort = 9000; // 本地机器上监听的端口
        const remotePort = 8000; // 远程主机将转发到本地端口的端口

        await agent.createReverseTunnel(remotePort, localHost, localPort);

        console.log('Reverse tunnel created. Listening on local port 9000 for remote connections on port 8000.');

        // 在本地启动服务模拟处理远程主机发送的数据
        const server = net.createServer((socket) => {
            console.log('Local server received a connection.');
            socket.on('data', (data) => {
                console.log('Received data from remote:', data.toString());
                socket.write('Hello, remote server!'); // 响应数据
            });
        });

        server.listen(localPort, localHost, () => {
            console.log(`Local server listening on ${localHost}:${localPort}`);
        });
    } catch (err) {
        console.error('Error:', err);
    }
}

main();
